package com.lucas.handler;

import com.lucsa.pojo.RpcRequest;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * 客户端是多线程.
 * 客户端也是需要handler去处理读和谐
 *
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private String result;
    private String param;//改造成rpcQuest的json字符串形式
    private ChannelHandlerContext ctx;
    //新增的参数
    private RpcRequest rpcRequest;



    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("=====channelActive======");
        this.ctx = ctx;
    }

    //接受服务端发送过的数据
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("=====channelRead======");
        this.result = msg.toString();
        System.out.println("接受了服务端的信息:"+msg.toString());
        notify();
    }

    @Override
    public synchronized Object call() throws Exception {
        System.out.println("=====call======");
        //从客户端发送消息给服务端
        ctx.writeAndFlush(rpcRequest);
        wait();
        return result;
    }


    public void setParam(String param) {
        this.param = param;
    }

    public RpcRequest getRpcRequest() {
        return rpcRequest;
    }

    public void setRpcRequest(RpcRequest rpcRequest) {
        this.rpcRequest = rpcRequest;
    }
}
